//
//  NSString+Normal.m
//  DS_PJ_01
//
//  Created by admin on 16/7/16.
//  Copyright © 2016年 zhiqiyun. All rights reserved.
//

#import "NSString+Normal.h"
#import <CommonCrypto/CommonCrypto.h>

@implementation NSString (Normal)

+ (NSString*)dataToJson:(id)object;
{
    NSString *jsonString = nil;
    NSError *error;
    
    
    if (!object) {
        NSAssert(1, @"字段为空");
        return @"";
    }
    if (!([object isKindOfClass:[NSDictionary class]]||[object isKindOfClass:[NSMutableDictionary class]]||[object isKindOfClass:[NSArray class]]||[object isKindOfClass:[NSMutableArray class]])) {
        
        NSAssert(1, @"object数据不是json格式数据");
        
        return @"";
    }
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object
                                                       options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                         error:&error];
    if (! jsonData) {
        NSLog(@"Got an error: %@", error);
    } else {
        jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    return jsonString;
}


-(NSString *)isNULL
{
    if ([self isKindOfClass:[NSNull class]] || self == nil) {
        return @"";
    } else {
        return self;
    }
}


- (NSString *)MD5Hash {
    if(self.length == 0) {
        return nil;
    }
    
    const char *cStr = [self UTF8String];
    unsigned char result[16];
    CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
    
    return [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],result[12], result[13], result[14], result[15]];
}




//DES
const Byte iv[] = {1,2,3,4,5,6,7,8};

-(NSString *)encryptUseDES
{
    return [self encryptUseDES_Key:des_key];
}

-(NSString *)decryptUseDES
{
    return [self decryptUseDES_Key:des_key];
}

//Des加密
-(NSString *) encryptUseDES_Key:(NSString *)key
{
    
    NSString *ciphertext = nil;
    NSData *textData = [self dataUsingEncoding:NSUTF8StringEncoding];
    NSUInteger dataLength = [textData length];
    unsigned char buffer[1024];
    memset(buffer, 0, sizeof(char));
    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES,
                                          kCCOptionPKCS7Padding,
                                          [key UTF8String], kCCKeySizeDES,
                                          iv,
                                          [textData bytes], dataLength,
                                          buffer, 1024,
                                          &numBytesEncrypted);
    if (cryptStatus == kCCSuccess) {
        NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted];
        ciphertext = [GTMBase64 stringByEncodingData:data];
    }
    return self;
}



//Des解密
-(NSString *)decryptUseDES_Key:(NSString *)key
{
    NSString *plaintext = nil;
    NSData *cipherdata = [GTMBase64 decodeString:self];
    unsigned char buffer[1024];
    memset(buffer, 0, sizeof(char));
    size_t numBytesDecrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmDES,
                                          kCCOptionPKCS7Padding,
                                          [key UTF8String], kCCKeySizeDES,
                                          iv,
                                          [cipherdata bytes], [cipherdata length],
                                          buffer, 1024,
                                          &numBytesDecrypted);
    if(cryptStatus == kCCSuccess)
    {
        NSData *plaindata = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesDecrypted];
        plaintext = [[NSString alloc]initWithData:plaindata encoding:NSUTF8StringEncoding];
    }
    return plaintext;
}




//关于NSUserDefaults
//存入一个值到NSUserDefaults
-(BOOL)saveValueToUserDefalutsWithKey:(NSString *)key
{
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    
    [ud setObject:self forKey:key];
    
    
    return [ud synchronize];
    
}

//使用方法 [@"Key" getUserDefaultsValue]，返回NSUserDefaults存入的值
-(id)getUserDefaultsValue
{
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    
    return [ud objectForKey:self];
}



@end
